package com.linyaonan.zuochengyun._01._03;

/**
 * @author: Lin
 * @date: 2022/7/24
 */
public class LeftDichotomize {

    public static void main(String[] args) {
        int [] org = {1,2,2,2,2,2,3,4,5};
        int i = find(org, 2);
        System.out.println(i);
    }

    public static int find(int[] org, int num) {
        if (org == null || org.length == 0) {
            return -1;
        }

        int ans = -1;
        int l = 0;
        int r = org.length - 1;

        while (l <= r) {
            int mid = l + (r - l) >> 1;
            // 找到的位置有可能不是最左边，所以首先记录下来，将查询范围锁定在l -> mid - 1
            if (org[mid] == num) {
                ans = mid;
                r = mid - 1;
            } else {
                if (org[mid] > num) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }
        }
        return ans;

    }

}
